import java.util.Stack;

public class Solution {
    //逆波兰表达式求值
    public int evalRPN(String[] tokens) {
        int n = tokens.length;
        Stack<Integer> stack = new Stack<>();
        for(int i = 0; i < n; i++) {
            int a = 0;
            int b = 0;
            String c = tokens[i];
            if(c.equals("+")) {
                a = stack.pop();
                b = stack.pop();
                stack.push(b + a);
            }
            else if(c.equals("-")) {
                a = stack.pop();
                b = stack.pop();
                stack.push(b - a);
            }
            else if(c.equals("*")) {
                a = stack.pop();
                b = stack.pop();
                stack.push(b * a);
            }
            else if(c.equals("/")) {
                a = stack.pop();
                b = stack.pop();
                stack.push(b / a);
            }
            else{
                stack.push(Integer.parseInt(tokens[i]));
            }
        }
        return stack.pop();
    }
}
